﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digit.Core;
using SMS.ServiceManager;
using SMS.UserManager;

public partial class Modules_DetailMatch : WebControlBase
{
    private long matchId = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        matchId = Request.QueryString["matchId"] != null ? Int32.Parse(Request.QueryString["matchId"].ToString().Trim()) : -1;

        if (ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1)
        {
            if (matchId == -1)
            {
                lnkAdd.Enabled = false;
                lnkAdd.Enabled = false;
            }
        }

        if (this.IsPostBack) return;

        if (matchId > 0)
        {
            this.BindObject();
        }

        this.BindTeamData();
    }

    private bool DoSave()
    {
        try
        {
            bool result = false;
            if (matchId > 0)
            {
                MatchInfo objMatch = MatchManager.Select(matchId);
                if (objMatch != null)
                {
                    objMatch.FinalScore = this.txtScore.Text.Trim();

                    objMatch.AsiaRate = this.txtAsiaRate.Text;
                    objMatch.AsiaHomeRate = this.txtAsiaHome.Text;
                    objMatch.AsiaAwayRate = this.txtAsiaAway.Text;

                    objMatch.TotalRate = this.txtTotalRate.Text;
                    objMatch.UpperRate = this.txtUpperRate.Text;
                    objMatch.LowerRate = this.txtLowerRate.Text;

                    objMatch.HomeTeamID = long.Parse(ddlHomeTeam.SelectedValue);
                    objMatch.AwayTeamID = long.Parse(ddlAwayTeam.SelectedValue);

                    objMatch.DatePlay = dtpDatePlay.SelectedDate;
                    objMatch.TimePlay = txtTime.Text;

                    objMatch.TeamPicked = this.txtPickTeam.Text;

                    objMatch.Status = byte.Parse(ddlStatus.SelectedValue);

                    objMatch.CreatedBy = "";
                    objMatch.CreatedDate = DateTime.Now;

                    if (rbtTrue.Checked == true)
                        objMatch.Win = 1;
                    else if (rbtFalse.Checked == true)
                        objMatch.Win = 2;
                    else if (rbtEqual.Checked == true)
                        objMatch.Win = 0;

                    result = MatchManager.Update(objMatch);

                    if (result) Response.Redirect("Default.aspx?module=ListMatch");
                    else
                        lblError.Text = "Thông tin chưa được cập nhật!";
                }
            }
            else
            {
                MatchInfo objMatch = new MatchInfo();

                objMatch.FinalScore = this.txtScore.Text.Trim();

                objMatch.AsiaRate = this.txtAsiaRate.Text;
                objMatch.AsiaHomeRate = this.txtAsiaHome.Text;
                objMatch.AsiaAwayRate = this.txtAsiaAway.Text;

                objMatch.TotalRate = this.txtTotalRate.Text;
                objMatch.UpperRate = this.txtUpperRate.Text;
                objMatch.LowerRate = this.txtLowerRate.Text;

                objMatch.HomeTeamID = long.Parse(ddlHomeTeam.SelectedValue);
                objMatch.AwayTeamID = long.Parse(ddlAwayTeam.SelectedValue);

                objMatch.TeamPicked = this.txtPickTeam.Text;

                objMatch.DatePlay = dtpDatePlay.SelectedDate;
                objMatch.TimePlay = txtTime.Text;

                objMatch.Status = byte.Parse(ddlStatus.SelectedValue);

                objMatch.CreatedBy = "";
                objMatch.CreatedDate = DateTime.Now;

                if (rbtTrue.Checked == true)
                    objMatch.Win = 1;
                else if (rbtFalse.Checked == true)
                    objMatch.Win = 2;
                else if (rbtEqual.Checked == true)
                    objMatch.Win = 0;

                result = MatchManager.Insert(objMatch);

                if (result) Response.Redirect("Default.aspx?module=ListMatch");
                else
                    lblError.Text = "Thông tin chưa được thêm mới!";
            }
            return result;
        }
        catch (Exception Ex)
        {
            this.lblError.Text = Ex.Message;
            return false;
        }
    }

    private void BindTeamData()
    {
        DataTable dt = TeamManager.SelectAll();

        if (dt != null && dt.Rows.Count > 0)
        {
            this.ddlHomeTeam.DataSource = dt;
            this.ddlHomeTeam.DataTextField = "TeamName";
            this.ddlHomeTeam.DataValueField = "TeamID";
            this.ddlHomeTeam.DataBind();

            this.ddlAwayTeam.DataSource = dt;
            this.ddlAwayTeam.DataTextField = "TeamName";
            this.ddlAwayTeam.DataValueField = "TeamID";
            this.ddlAwayTeam.DataBind();

            HomeTeamID = Convert.ToInt64(ddlHomeTeam.SelectedValue);

            AwayTeamID = Convert.ToInt64(ddlAwayTeam.SelectedValue);
        }
        else
        {
            this.ddlHomeTeam.DataSource = null;
            this.ddlHomeTeam.DataBind();
            this.ddlAwayTeam.DataSource = null;
            this.ddlAwayTeam.DataBind();
        }
    }

    private long HomeTeamID
    {
        get
        {
            if (this.ViewState["vsHomeTeamID"] != null)
                return (long)this.ViewState["vsHomeTeamID"];
            else
                return -1;
        }

        set { this.ViewState["vsHomeTeamID"] = value; }
    }

    private long AwayTeamID
    {
        get
        {
            if (this.ViewState["vsAwayTeamID"] != null)
                return (long)this.ViewState["vsAwayTeamID"];
            else
                return -1;
        }

        set { this.ViewState["vsAwayTeamID"] = value; }
    }

    private void BindObject()
    {
        MatchInfo objMatch = MatchManager.Select(matchId);

        if (objMatch != null)
        {
            this.ddlHomeTeam.SelectedValue = objMatch.HomeTeamID.ToString();
            this.ddlAwayTeam.SelectedValue = objMatch.AwayTeamID.ToString();

            this.dtpDatePlay.Text = objMatch.DatePlay.ToString("dd/MM/yyyy");
            this.txtTime.Text = objMatch.TimePlay;
            this.txtScore.Text = objMatch.FinalScore;

            this.txtAsiaRate.Text = objMatch.AsiaRate;
            this.txtAsiaHome.Text = objMatch.AsiaHomeRate;
            this.txtAsiaAway.Text = objMatch.AsiaAwayRate;

            this.txtTotalRate.Text = objMatch.TotalRate;
            this.txtUpperRate.Text = objMatch.UpperRate;
            this.txtLowerRate.Text = objMatch.LowerRate;

            this.txtPickTeam.Text = objMatch.TeamPicked;
            this.txtScore.Text = objMatch.FinalScore;

            if (objMatch.Win == 0)
                rbtEqual.Checked = true;
            else if (objMatch.Win == 1)
                rbtTrue.Checked = true;
            else if (objMatch.Win == 2)
                rbtFalse.Checked = true;

            ddlStatus.SelectedIndex = objMatch.Status;
        }
    }

    protected void lnkCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?module=ListMatch");
    }

    protected void lnkAdd_Click(object sender, EventArgs e)
    {
        try
        {
            DoSave();
        }
        catch { }
    }
}
